package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.WebNavigationDao;
import ysq.xyg.model.WebNavigation;
import ysq.xyg.service.WebNavigationService;
import ysq.xyg.util.BaseConstant;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Yu Shengqiang on 2017/5/3.
 */
@Service("webNavigationService")
public class WebNavigationServiceImpl extends BaseServiceImpl<WebNavigation> implements WebNavigationService {
    @Resource
    private WebNavigationDao webNavigationDao;
    @Autowired
    public void setWebNavigationDao(WebNavigationDao webNavigationDao) {
        this.baseDao = webNavigationDao;
    }
    //分页查询
    public PageInfo<WebNavigation> page(WebNavigation webNavigation, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("sort desc");
        return new PageInfo<WebNavigation>(list(webNavigation));
    }
    //查询
    public List<WebNavigation> list(WebNavigation webNavigation) {
        //通用Example查询
        Example example = new Example(WebNavigation.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webNavigation.getName())){
            criteria.andLike("name","%"+webNavigation.getName()+"%");
        }
        if(StringUtil.isNotEmpty(webNavigation.getParentId())){
            criteria.andEqualTo("parentId",webNavigation.getParentId());
        }
        if(StringUtil.isNotEmpty(webNavigation.getUseType())){
            criteria.andEqualTo("useType",webNavigation.getUseType());
        }
        if(StringUtil.isNotEmpty(webNavigation.getType())){
            criteria.andEqualTo("type",webNavigation.getType());
        }
        example.setOrderByClause(" sort desc");
        return webNavigationDao.selectByExample(example);
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(WebNavigation webNavigation){
        return webNavigationDao.AcceptMaxCode(webNavigation);
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(String menuCode){
        WebNavigation webNavigation=new WebNavigation();
        if (StringUtil.isNotEmpty(menuCode)){
            webNavigation.setCode(menuCode);
        }
        webNavigation.setGrade(BaseConstant.MENU_ONE);
        String menuCodeMax =webNavigationDao.AcceptMaxCode(webNavigation);
        if(StringUtil.isEmpty(menuCodeMax)){
            menuCodeMax=BaseConstant.MENU_CODE_MIN;
        }else{
            menuCodeMax=Long.parseLong(menuCodeMax)+1+"";
        }
        return menuCodeMax;
    }

    /***
     * 菜单左查询
     * @param webNavigation
     * @return
     */
    public List<WebNavigation> selectEntityByLeft(WebNavigation webNavigation){
        return webNavigationDao.selectAll();
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(String parentId,String menuGrade){
        WebNavigation webNavigation=new WebNavigation();
        webNavigation.setParentId(parentId);
        webNavigation.setGrade(menuGrade);
        String menuCodeMax =webNavigationDao.AcceptMaxCode(webNavigation);
        if(StringUtil.isEmpty(menuCodeMax)){
            menuCodeMax=BaseConstant.MENU_CODE_MIN;
        }else{
            menuCodeMax=Long.parseLong(menuCodeMax)+1+"";
        }
        return menuCodeMax;
    }
}
